package com.better.common.mongo.model;

import com.better.common.utils.GlobalUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by xiaq on 2017/9/11.
 */
@Document
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("基础实体对象")
public class BaseModel implements Serializable {
    private static final long serialVersionUID = -3258839839160856613L;
    @Id
    @ApiModelProperty(hidden = true)
    private String id;
    @ApiModelProperty(hidden = true)
    private String create_by;
    @ApiModelProperty(hidden = true)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date create_time;
    @ApiModelProperty(hidden = true)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date update_time;
    @ApiModelProperty(hidden = true)
    private String state;
    public void preInsert(){
        if(StringUtils.isEmpty(this.id)){
            this.setId(GlobalUtil.createGlobalId());
        }
        if(this.getCreate_time()==null){
            this.setCreate_time(new Date());
        }
        this.setUpdate_time(new Date());
    }
    public void preUpdate(){
        this.setUpdate_time(new Date());
    }
}
